<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>跨域问题</title>
    <script src="jquery-1.11.3.js"></script>
    <link rel="stylesheet" type="text/css" href="jasmine-2.8.0/jasmine.css">

    <script src="jasmine-2.8.0/jasmine.js"></script>
    <script src="jasmine-2.8.0/jasmine-html.js"></script>
    <script src="jasmine-2.8.0/boot.js"></script>
</head>
<body>

<script>

    // 每一个测试用例的超时时间
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

    describe("跨域测试", function () {

        // 测试方法
        it("getOk请求", function (done) {
            // 服务器返回的结果
            var obj;

            $.getJSON("http://localhost:8081/testController/getOk").then(function (result) {
                console.log(result);
                obj = result;
            });
            setTimeout(function () {
                expect(obj).toEqual({
                    "data": "getOk"
                });
                done();
            }, 100);
        });


        it("getCookie请求", function (done) {
            // 服务器返回的结果
            var result;

            $.ajax({
                type: "get",
                url: "http://localhost:8081/testController/getCookie",
                xhrFields: {
                    withCredentials: true
                },
                success: function (json) {
                    result = json;
                }
            });

            // 由于是异步请求，需要使用setTimeout来校验
            setTimeout(function () {
                expect(result).toEqual({
                    "data": "getCookie"
                });

                // 校验完成，通知jasmine框架
                done();
            }, 100);
        });


        it("postOk请求", function (done) {
            // 服务器返回的结果
            var obj;

            $.ajax({
                type: "post",
                url: "http://localhost:8081/testController/postOk",
                contentType: "application/json;charset=utf-8",
                success: function (result) {
                    obj = result;
                }
            });

            setTimeout(function () {
                expect(obj).toEqual({
                    "data": "postOk"
                });
                done();
            }, 100);
        });


        it("getHeader请求", function(done) {
            // 服务器返回的结果
            var result;

            $.ajax({
                type : "get",
                url: "http://localhost:8081/testController/getHeader",
                headers:{
                    "x-header1" : "AAA"
                },
                beforeSend: function(xhr){
                    xhr.setRequestHeader("x-header2","BBB")
                },
                success: function(json){
                    result = json;
                }
            });

            // 由于是异步请求，需要使用setTimeout来校验
            setTimeout(function() {
                expect(result).toEqual({
                    "data" : "getHeader"
                });

                // 校验完成，通知jasmine框架
                done();
            }, 100);
        });

        it("delete请求", function(done) {
            // 服务器返回的结果
            var result;

            $.ajax({
                type : "delete",
                url: "http://localhost:8081/testController/deleteRequest",
                success: function(json){
                    result = json;
                }
            });

            // 由于是异步请求，需要使用setTimeout来校验
            setTimeout(function() {
                expect(result).toEqual({
                    "data" : "deleteRequest"
                });

                // 校验完成，通知jasmine框架
                done();
            }, 100);
        });



    });

</script>

</body>
</html>